-- package.cpath = package.cpath .. ";../luaclib/?.so"
-- package.path = package.path .. ";../lualib/?.lua"

-- if _VERSION ~= "Lua 5.4" then
--     error "Use lua 5.4"
-- end

-- local socket = require "socket"
-- local cjsonhelper = require "cjsonhelper"
-- local stringhelper = require "stringhelper"
-- local pbhelper = require "pbhelper"

-- local nPrefixLen = 2
-- local szFormat = string.format("> i%d", nPrefixLen)
-- -- 创建 TCP 客户端套接字
-- local client = socket.tcp()
-- -- 连接到服务器
-- client:connect("127.0.0.1", 8001)

-- local pbMap = {
--     login = "login.loginmsg"
-- }

-- function processLoginMsg(msgArr)
--     local msg = {}
--     msg.cmd = msgArr[1]
--     msg.username = msgArr[2]
--     msg.pwd = msgArr[3]

--     return msg
-- end

-- local processMap = {
--     login = processLoginMsg
-- }

-- function recvMsg()
--     local head = client:receive(nPrefixLen)
--     if head then
--         local len = string.unpack(szFormat, head)
--         print("len:", len)
--         local body = client:receive(len)
--         -- local unpack_msg = cjsonhelper.json_unpack_no_prefix(body)
--         local unpack_msg = pbhelper.decode(body)
--         local szmsg = ""
--         for i, v in pairs(unpack_msg) do
--             szmsg = szmsg .. " " .. i .. " " .. v .. " "
--         end
--         print(szmsg)
--         return true
--     else
--         print("recvMsg ERROR")
--         return false
--     end

-- end

-- function sendMsg(msg)

--     -- local pack = cjsonhelper.json_pack_with_prefix(msg)
--     local cmd = msg[1] or ""
--     if not cmd then
--         print("输入错误")
--         return true
--     end
--     local pbType = pbMap[cmd]
--     if not pbType then
--         print("没有注册此类型cmd", cmd)
--         return true
--     end

--     local f = processMap[cmd]
--     if not f then
--         print("没有注册此类型cmd的处理函数", cmd)
--         return true
--     end

--     local sendmsg = f(msg)

--     local pbpack = pbhelper.encode(pbType, sendmsg)
--     local format = string.format("> i2 c%d", string.len(pbpack))
--     local pack = string.pack(format, string.len(pbpack), pbpack)
--     local bOK = client:send(pack)
--     if not bOK then
--         return false
--     end

--     return true
-- end

-- function test1()

--     local bClosed = false

--     pbhelper.registerFile("../proto/login.pb")
--     while not bClosed do
--         local input = io.read()
--         if input == "bye" then
--             break
--         end

--         if input == "r" then
--             if not recvMsg() then
--                 break
--             end
--         else
--             local msgArr = stringhelper.split(input, ",")
--             if not sendMsg(msgArr) then
--                 break
--             end
--         end
--     end

--     -- 关闭连接
--     client:close()
-- end

-- function test2()
--     local msg = {"login", "111", "123"}
--     local pack = cjsonhelper.json_pack_no_prefix(msg)

--     print("pack:", pack, string.len(pack))
--     local unp = cjsonhelper.json_unpack_no_prefix(pack)
--     print("unpack", unp, unp[1], unp[2], unp[3])
-- end

-- function test3()
--     local msg = "abcdE,Faa,,"
--     local t = stringhelper.split(msg, ",")
--     for _, v in pairs(t) do
--         print(v)
--     end

--     print(t)
--     local pack = cjsonhelper.json_pack_with_prefix(t)

--     print(table.concat({}, ","))
-- end

-- function test4()
--     pbhelper.registerFile("../proto/error.pb")

--     local erroRet = {
--         cmd = "error",
--         status = -1,
--         msg = "错误"
--     }

--     local pack = pbhelper.encode("error.errorRet", erroRet)
--     local unpack = pbhelper.decode(pack)
-- end

-- -- test1()
-- -- test2()
-- -- test3()
-- test4()
